package com.ruoyi.report_sj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sj.domain.bo.ReportSjnjsjBaDetailBo;
import com.ruoyi.report_sj.domain.vo.ReportSjnjsjBaDetailVo;
import com.ruoyi.report_sj.domain.ReportSjnjsjBaDetail;
import com.ruoyi.report_sj.mapper.ReportSjnjsjBaDetailMapper;
import com.ruoyi.report_sj.service.IReportSjnjsjBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 砂浆凝结时间试验检测记录详情Service业务层处理
 *
 * @author zyb
 * @date 2024-05-29
 */
@RequiredArgsConstructor
@Service
public class ReportSjnjsjBaDetailServiceImpl implements IReportSjnjsjBaDetailService {

    private final ReportSjnjsjBaDetailMapper baseMapper;

    /**
     * 查询砂浆凝结时间试验检测记录详情
     */
    @Override
    public ReportSjnjsjBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询砂浆凝结时间试验检测记录详情列表
     */
    @Override
    public TableDataInfo<ReportSjnjsjBaDetailVo> queryPageList(ReportSjnjsjBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSjnjsjBaDetail> lqw = buildQueryWrapper(bo);
        Page<ReportSjnjsjBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询砂浆凝结时间试验检测记录详情列表
     */
    @Override
    public List<ReportSjnjsjBaDetailVo> queryList(ReportSjnjsjBaDetailBo bo) {
        LambdaQueryWrapper<ReportSjnjsjBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSjnjsjBaDetail> buildQueryWrapper(ReportSjnjsjBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSjnjsjBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, ReportSjnjsjBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getBianh() != null, ReportSjnjsjBaDetail::getBianh, bo.getBianh());
        lqw.eq(bo.getCessj1() != null, ReportSjnjsjBaDetail::getCessj1, bo.getCessj1());
        lqw.eq(bo.getCessj2() != null, ReportSjnjsjBaDetail::getCessj2, bo.getCessj2());
        lqw.eq(bo.getLis1() != null, ReportSjnjsjBaDetail::getLis1, bo.getLis1());
        lqw.eq(bo.getLis2() != null, ReportSjnjsjBaDetail::getLis2, bo.getLis2());
        lqw.eq(bo.getCezjmj1() != null, ReportSjnjsjBaDetail::getCezjmj1, bo.getCezjmj1());
        lqw.eq(bo.getCezjmj2() != null, ReportSjnjsjBaDetail::getCezjmj2, bo.getCezjmj2());
        lqw.eq(bo.getHuansxs() != null, ReportSjnjsjBaDetail::getHuansxs, bo.getHuansxs());
        lqw.eq(bo.getGuanryl1() != null, ReportSjnjsjBaDetail::getGuanryl1, bo.getGuanryl1());
        lqw.eq(bo.getGuanryl2() != null, ReportSjnjsjBaDetail::getGuanryl2, bo.getGuanryl2());
        lqw.eq(bo.getDanwmjgrzl1() != null, ReportSjnjsjBaDetail::getDanwmjgrzl1, bo.getDanwmjgrzl1());
        lqw.eq(bo.getDanwmjgrzl2() != null, ReportSjnjsjBaDetail::getDanwmjgrzl2, bo.getDanwmjgrzl2());
        return lqw;
    }

    /**
     * 新增砂浆凝结时间试验检测记录详情
     */
    @Override
    public Boolean insertByBo(ReportSjnjsjBaDetailBo bo) {
        ReportSjnjsjBaDetail add = BeanUtil.toBean(bo, ReportSjnjsjBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改砂浆凝结时间试验检测记录详情
     */
    @Override
    public Boolean updateByBo(ReportSjnjsjBaDetailBo bo) {
        ReportSjnjsjBaDetail update = BeanUtil.toBean(bo, ReportSjnjsjBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSjnjsjBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除砂浆凝结时间试验检测记录详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
